package com.example.hhh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hhh.model.pojo.Staff;
import com.example.hhh.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    private StaffService staffService;

    @PostMapping("/register")
    public ResponseEntity<?> registerUser(@RequestBody Map<String, String> data) {
        String username = data.get("username");
        String password = data.get("password");
        String realName = data.get("realName");
        String email = data.get("email");

        // 检查用户名或邮箱是否已存在
        if (staffService.getOne(new QueryWrapper<Staff>().eq("username", username)) != null) {
            return ResponseEntity.status(HttpStatus.CONFLICT).body("用户名已存在");
        }
        if (staffService.getOne(new QueryWrapper<Staff>().eq("email", email)) != null) {
            return ResponseEntity.status(HttpStatus.CONFLICT).body("邮箱已存在");
        }
        Staff staff = new Staff();
        staff.setUsername(username);
        staff.setPassword(password);
        staff.setRealName(realName);
        staff.setEmail(email);
        staff.setRole("普通员工"); // 普通员工
        staff.setStatus(1); // 在职
        staff.setCreateTime(new java.util.Date());
        staffService.save(staff);
        return ResponseEntity.ok("注册成功");
    }

    @PostMapping("/login")
    public ResponseEntity<?> loginUser(@RequestBody Map<String, String> credentials) {
        String username = credentials.get("username");
        String password = credentials.get("password");
        Staff staff = staffService.getOne(new QueryWrapper<Staff>().eq("username", username));
        if (staff == null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户不存在");
        }
        if (!password.equals(staff.getPassword())) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("密码错误");
        }
        
        // 更新最后登录时间
        staff.setLastLoginTime(new java.util.Date());
        staffService.updateById(staff);
        
        // 登录成功，返回用户信息和token
        Map<String, Object> data = new HashMap<>();
        data.put("token", "mock-token-for-" + username);
        Map<String, Object> userMap = new HashMap<>();
        userMap.put("staffId", staff.getStaffId());
        userMap.put("username", staff.getUsername());
        userMap.put("realName", staff.getRealName());
        userMap.put("department", staff.getDepartment());
        userMap.put("position", staff.getPosition());
        userMap.put("phone", staff.getPhone());
        userMap.put("email", staff.getEmail());
        userMap.put("role", staff.getRole());
        userMap.put("roleName", staff.getRoleName());
        userMap.put("status", staff.getStatus());
        userMap.put("lastLoginTime", staff.getLastLoginTime());
        userMap.put("createTime", staff.getCreateTime());
        data.put("user", userMap);
        return ResponseEntity.ok(data);
    }

    @PostMapping("/logout")
    public ResponseEntity<?> logout() {
        Map<String, Object> data = new HashMap<>();
        data.put("message", "退出登录成功");
        return ResponseEntity.ok(data);
    }
} 